import 'package:flutter/material.dart';
import 'package:second_hand/pages/home/HomePage/recommend_page.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(
      length: 10,
      vsync: this,
    ); // 初始化 TabController
  }

  @override
  void dispose() {
    _tabController.dispose(); // 释放 TabController
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // backgroundColor: Colors.white,
      body: Column(
        children: <Widget>[
          Container(
            color: Colors.white,
            child: Column(
              children: <Widget>[
                const Padding(padding: EdgeInsets.all(15.0)),
                SearchBar(),
                CustomTabBar(tabController: _tabController),
              ],
            ),
          ),
          // 传递 TabController
          Expanded(
            child: TabBarView(
              controller: _tabController, // 为 TabBarView 设置控制器
              children: [
                RecommendList(), // 每个标签对应的内容
                Text("校园"),
                Text("教材"),
                Text("电子"),
                Text("日用"),
                Text("1"),
                Text("2"),
                Text("3"),
                Text("5"),
                Text("6"),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class SearchBar extends StatelessWidget {
  const SearchBar({super.key});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.fromLTRB(15, 15, 15, 10),
      child: Container(
        color: Colors.white,
        height: 35.0,
        child: Row(
          children: [
            Expanded(
              child: TextField(
                decoration: InputDecoration(
                  prefixIcon: const Icon(Icons.search),
                  hintText: '搜索商品',
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(20.0),
                  ),
                ),
              ),
            ),
            const Padding(padding: EdgeInsets.only(left: 10)),
            GestureDetector(
              child: SizedBox(
                height: 30,
                child: const Icon(Icons.crop_free_rounded,size: 32,),
              ),

              onTap: () {
                //todo
                // 筛选按钮点击事件
              },
            ),
          ],
        ),
      ),
    );
  }
}

class CustomTabBar extends StatelessWidget {
  final TabController tabController;

  const CustomTabBar({super.key, required this.tabController});

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 30,
      child: TabBar(
        tabAlignment: TabAlignment.start,
        controller: tabController,
        indicatorColor: Colors.blue,
        labelColor: Colors.blue,
        unselectedLabelColor: Colors.black,
        isScrollable: true,
        tabs: [
          Tab(
            child: Text(
              '推荐',
              style: TextStyle(fontSize: 18), // 设置字体大小
            ),
          ),
          Tab(child: Text('校园', style: TextStyle(fontSize: 18))),
          Tab(child: Text('教材', style: TextStyle(fontSize: 18))),
          Tab(child: Text('电子', style: TextStyle(fontSize: 18))),
          Tab(child: Text('日用', style: TextStyle(fontSize: 18))),
          Tab(child: Text('1', style: TextStyle(fontSize: 18))),
          Tab(child: Text('2', style: TextStyle(fontSize: 18))),
          Tab(child: Text('3', style: TextStyle(fontSize: 18))),
          Tab(child: Text('5', style: TextStyle(fontSize: 18))),
          Tab(child: Text('6', style: TextStyle(fontSize: 18))),
        ],
      ),
    );
  }
}
